# 10_jes2014_wave10.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")
require("plink")


# dataset
JES2014 <- read_sav(
  "JESV第10･11波2014年衆院選事前・事後調査統合.sav", 
  user_na = FALSE
)


# respondent
JES2014_Respondent <- JES2014[,c("id", paste0("a", 20:29, "x1"))]
colnames(JES2014_Respondent)[-1] <- paste0("q", 20:29)


# IRT
## handle missing data
JES2014_Respondent <- JES2014_Respondent[rowSums(JES2014_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2014 <- ltm::grm(JES2014_Respondent[,-1], IRT.param = FALSE)
irt2014 ## viewing

## estimate voters ideology
score2014_Respondent <- ltm::factor.scores(irt2014, JES2014_Respondent[,-1])
JES2014_Respondent$Ideology <- -1 * score2014_Respondent$score.dat$z1


# rescale
## common item
common2012 <- c("q8", "q9", "q10", "q11", "q12", "q13", "q14")
common2014 <- c("q22", "q23", "q24", "q25", "q26", "q27", "q28")
index <- data.frame(
  group1 = match(common2012, colnames(JES2012_Respondent[,-1])), 
  group2 = match(common2014, colnames(JES2014_Respondent[,-1]))
)

## list of parameters
### 2012
res2012 <- coef(irt2012)
res2012 <- res2012[,c(4,1:3)]
colnames(res2012) <- c("a","b1","b2","b3")
### 2014
res2014 <- coef(irt2014)
res2014 <- res2014[,c(4,1:3)]
colnames(res2014) <- c("a","b1","b2","b3")
### merge
pm <- list(res2012, res2014)

## list of scale
rescat <- list(rep(4, nrow(res2012)), 
               rep(4, nrow(res2014)))

## as.irt.pars
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
pm2014 <- as.poly.mod(n = nrow(res2014), model = "grm", 
                      items = 1:nrow(res2014))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2012, pm2014), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2014_Respondent$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2014_Respondent_plink <- link.ability(plink_out)$group2
JES2014_Respondent$Ideology <- -1 * score2014_Respondent_plink


# LDP's position
JES2014_LDP <- JES2014[,c("id", paste0("a", 20:29, "x3_1"))]
colnames(JES2014_LDP)[-1] <- paste0("q", 20:29)
JES2014_LDP <- JES2014_LDP[rowSums(JES2014_LDP[,-1], na.rm = TRUE) != 0,]
score2014_LDP <- ltm::factor.scores(irt2014, JES2014_LDP[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2014_LDP$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2014_LDP$Ideology <- -1 * link.ability(plink_out)$group2


# DPJ's position
JES2014_DPJ <- JES2014[,c("id", paste0("a", 20:29, "x3_2"))]
colnames(JES2014_DPJ)[-1] <- paste0("q", 20:29)
JES2014_DPJ <- JES2014_DPJ[rowSums(JES2014_DPJ[,-1], na.rm = TRUE) != 0,]
score2014_DPJ <- ltm::factor.scores(irt2014, JES2014_DPJ[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2014_DPJ$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2014_DPJ$Ideology <- -1 * link.ability(plink_out)$group2
